package com.main.common.utils;

import com.main.common.enums.SaTokenKey;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.TimeZone;

@Slf4j
@Component
public class ZoneDateTimeConvert {

    private final String systemTimeZone =  ConfigUtil.getProperty("Default.TimeZone");

    private ZoneId sessionTimeZone;

    SaTokenUtil saTokenUtil = new SaTokenUtil();

    public LocalDateTime convertToSession(LocalDateTime localDateTime) {
        if(localDateTime == null) {
            return null;
        }
       log.info(String.valueOf(saTokenUtil.getSession()));
        sessionTimeZone = saTokenUtil.getBySessionKey(SaTokenKey.SESSION_TIME_ZONE, ZoneId.class);
        if(sessionTimeZone == null) {
            sessionTimeZone = ZoneId.of(systemTimeZone);
        }
        localDateTime = localDateTime.atZone(ZoneId.of(systemTimeZone)).withZoneSameInstant(sessionTimeZone).toLocalDateTime();
        return localDateTime;
    }
    public LocalDateTime convertToSystem(LocalDateTime localDateTime) {
        if(localDateTime == null) {
            return null;
        }
       log.info(String.valueOf(saTokenUtil.getSession()));
        sessionTimeZone = saTokenUtil.getBySessionKey(SaTokenKey.SESSION_TIME_ZONE, ZoneId.class);
        if(sessionTimeZone == null) {
            sessionTimeZone = ZoneId.of(systemTimeZone);
        }
        localDateTime = localDateTime.atZone(sessionTimeZone).withZoneSameInstant(ZoneId.of(systemTimeZone)).toLocalDateTime();
        return localDateTime;
    }

    public LocalDateTime sessionToSystem(LocalDateTime localDateTime) {
        if(localDateTime == null) {
            return null;
        }
       log.info(String.valueOf(saTokenUtil.getSession()));
        sessionTimeZone = saTokenUtil.getBySessionKey(SaTokenKey.SESSION_TIME_ZONE, ZoneId.class);
        if(systemTimeZone == null){
            return localDateTime;
        }
        if(sessionTimeZone == null) {
            sessionTimeZone = ZoneId.of(systemTimeZone);
        }
        localDateTime = localDateTime.atZone(sessionTimeZone).withZoneSameInstant(ZoneId.of(systemTimeZone)).toLocalDateTime();
        return localDateTime;
    }

    public LocalDateTime systemToSession(LocalDateTime localDateTime) {
        if(localDateTime == null) {
            return null;
        }
       log.info(String.valueOf(saTokenUtil.getSession()));
        sessionTimeZone = saTokenUtil.getBySessionKey(SaTokenKey.SESSION_TIME_ZONE, ZoneId.class);
        if(systemTimeZone == null){
            return localDateTime;
        }
        if(sessionTimeZone == null) {
            sessionTimeZone = ZoneId.of(systemTimeZone);
        }
        localDateTime = localDateTime.atZone(ZoneId.of(systemTimeZone)).withZoneSameInstant(sessionTimeZone).toLocalDateTime();
        return localDateTime;
    }

    public LocalDateTime localToSystem() {
        LocalDateTime localDateTime = LocalDateTime.now();
        ZoneId defaultTimeZone = ZoneId.systemDefault();;
       log.info(String.valueOf(saTokenUtil.getSession()));
        sessionTimeZone = saTokenUtil.getBySessionKey(SaTokenKey.SESSION_TIME_ZONE, ZoneId.class);
        if(systemTimeZone == null){
            return localDateTime;
        }
        localDateTime = localDateTime.atZone(defaultTimeZone).withZoneSameInstant(ZoneId.of(systemTimeZone)).toLocalDateTime();
        return localDateTime;
    }
}
